package com.smh.a04objectStreamDemo1;

import java.io.*;

public class ObjectStreamDemo3 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {


        Student s1 = new Student("zhangsan", 13, "nan", "hebei");
        Student s2 = new Student("lisi", 15, "nan", "hebei");
        Student s3 = new Student("wangwu", 32, "nan", "hebei");
        Student s4 = new Student("maliu", 23, "nan", "hebei");

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day27-code\\file\\demo2\\b.txt"));

        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.writeObject(s3);
        oos.writeObject(s4);
        oos.close();

        //读取多个自定义对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day27-code\\file\\demo2\\b.txt"));

        Object o;
        //这里其实是没用的,因为ois读不到后会直接报异常
        while ((o = ois.readObject()) != null) {
            System.out.println(o);
        }

        ois.close();

        //解决方法,存的时候直接存一个ArrayList集合,读的时候只需要读List集合就行.
    }
}
